//
//  CinemaViewController.m
//  newDouban
//
//  Created by dllo on 15/10/8.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CinemaViewController.h"
#import "cinemaTableViewCell.h"
#import "AppTools.h"
#import "Cinema.h"
#import "MBProgressHUD.h"
#import "DataBaseSingTon.h"
#import "MJRefresh.h"
#import "Reachability.h"

@interface CinemaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *cinemaTableV;
@property (nonatomic, retain) NSMutableArray *cinemaArray;
@property (nonatomic, retain) MBProgressHUD *hub;

@end

@implementation CinemaViewController

- (void)dealloc
{
    [_hub release];
    [_cinemaArray release];
    [_cinemaTableV release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.tintColor = [UIColor greenColor];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.tintColor = [UIColor greenColor];
    self.navigationItem.title = @"影院";
    self.cinemaTableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.cinemaTableV.delegate = self;
    self.cinemaTableV.dataSource = self;
    self.cinemaTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.cinemaTableV registerClass:[cinemaTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.cinemaTableV];
    [_cinemaTableV release];
    
    [self openDB];
   
    self.cinemaArray = [NSMutableArray array];
    
    NSMutableArray *arrM = [self selectCinema];
    self.cinemaArray = [NSMutableArray arrayWithArray:arrM];

    
    [self.cinemaTableV addHeaderWithCallback:^{
        
        NSLog(@"下拉");
        [self freash];
        
    }];
    [self.cinemaTableV headerBeginRefreshing];
    

}


- (void)UIAlerController
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"网路连接出错" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}


- (void)freash
{
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php";
    
    
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    switch ([r currentReachabilityStatus]) {
        case NotReachable:
            NSLog(@"没有网络连接");
            // 没有网络连接
            [self UIAlerController];
            [self.cinemaTableV headerEndRefreshing];
            
            break;
        case ReachableViaWWAN:
            // 使用3G网络
            NSLog(@"使用3G网络");
            
            break;
        case ReachableViaWiFi:
            // 使用WiFi网络
            NSLog(@"使用WiFi网络");
            
            self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [self.hub show:YES];
            
            self.hub.labelText = @"正在加载中....";
            
            break;
    }

    

    
//    self.cinemaArray = [NSMutableArray array];
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        //
        [self dropTable];
        [self createTable];
        NSMutableDictionary *dic = result;
        NSDictionary *dicSecond = [dic objectForKey:@"result"];
        NSMutableArray *array = [dicSecond objectForKey:@"data"];
        for (NSDictionary *dicTemp in array) {
            Cinema *cinema = [[Cinema alloc] init];
            [cinema setValuesForKeysWithDictionary:dicTemp];
            [self.cinemaArray addObject:cinema];
            [self insertCinema:cinema];
            [cinema release];
        }
        [self.cinemaTableV headerEndRefreshing];
        [self.cinemaTableV reloadData];
        [self.hub hide:YES];
        
        
    }];

}

//打开数据
- (void)openDB
{
    [[DataBaseSingTon shareDataBase] openDB];
}

//创建表
- (void)createTable
{
    [[DataBaseSingTon shareDataBase] createTable];
}
//插入数据
- (void)insertCinema:(Cinema *)cinema
{

    
    [[DataBaseSingTon shareDataBase] insertCinema:cinema];
}
//查询数据
- (NSMutableArray *)selectCinema
{
    NSMutableArray *arrM = [[DataBaseSingTon shareDataBase] selectCinema];
    
    return arrM;

}
//删除表
- (void)dropTable
{
    [[DataBaseSingTon shareDataBase] dropTable];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 220;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.cinemaArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    cinemaTableViewCell *cell = [[[cinemaTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
    
    static NSString *cellIndentifier = @"cell";
    cinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    
    Cinema *cinema = [self.cinemaArray objectAtIndex:indexPath.row];
//    NSLog(@"cinema.address =================================================================================================================== %@", cinema.address);
    cell.cinema = cinema;

    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
